# coding=utf-8
"""
执行方式：python -m unittest tAuthBook.AuthBookDemo
"""
# 导入测试模块
import unittest

from cAuthBook import app, db, Author, Book


# 自定义测试类
class AuthBookDemo(unittest.TestCase):
    def setUp(self):
        self.app = app
        db.create_all()

    def tearDown(self):
        # 清除数据库会话对象
        db.session.remove()
        # 清除数据库
        db.drop_all()

    def test_add_data(self):
        # 添加数据
        auth = Author(name='hehe')
        book = Book(info='python')
        # 提交数据到数据库
        db.session.add_all([auth, book])
        db.session.commit()
        # 提交数据后需要再次查询
        au = Author.query.filter_by(name='hehe').first()
        bk = Book.query.filter_by(info='python').first()
        # 断言数据存在
        self.assertIsNotNone(au)
        self.assertIsNotNone(bk)
